<?php
namespace module\app\controller;

use app\Controller;
use app\model\ActivityEnroll as Model;
use app\model\City;

class ActivityEnroll extends Controller
{
    /**
     * 列表.
     * 
     */
    public function index()
    {
        $result = Model::scope('user')->withSearch(['status'], input('get.'))->order('update_at', 'DESC')
            ->field(['id', 'activity_id', 'user_id', 'status'])->paginate([
                'list_rows'=> input('get.pageSize/d', 10),
            ]);
        return json([
            'items' => $result->items(),
            'total' => $result->total()
        ]);        
    }
    /**
     * 详情.
     * 
     */
    public function read(int $id)
    {
        $model = Model::with(['activity', 'user'])->scope('user')->findOrFail($id);
        $model->areaText = City::labels($model->dist_id, ['value' => 'code', 'label' => 'name']);
        return json($model->toArray());    
    }
    /**
     * 保存.
     * 
     */
    public function save()
    {
        $model = new Model;
        $model->save($this->request->post());
        return json($model->toArray());
    }
    /**
     * 删除.
     * 
     * @param int $id
     */
    public function del(int $id)
    {
        $model = Model::scope('user')->findOrFail($id);
        $model->delete();
        return json($model->toArray());
    }
}
